var crypto = require('crypto');
var User = require('../models/user.js');
var Smtp = require('../models/smtp.js');
/*
 * homepage.
 */
exports.index = function(req, res){
    console.log("index");
    if(req.session.user){
        if(req.session.user.state ===0)
            return res.redirect('/checkmail');
        else
            return res.redirect('/u/'+req.session.user.mail);
    }

    res.render('index', {
        title: '首页',
        user : req.session.user,                    //aboute cookie
        success : req.flash('success').toString(),
        error : req.flash('error').toString()
    });
};

/*
 * reg
 */
exports.reg = function(req, res) {
    res.render('reg', {
        title: '用户注册',
        user : req.session.user,
        success : req.flash('success').toString(),
        error : req.flash('error').toString()
    });
};

/*
 * doReg
 */
exports.doReg = function(req, res) {
    //检查密码
    if (req.body['password-repeat'] != req.body['password']) {
        req.flash('error', '两次输入的密码不一致');
        return res.redirect('/reg');
    }

    //var regexPW = /^[\@A-Za-z0-9\!\#\$\%\^\&\*\.\~]{6,40}$/;
    var regexPW = /^.{6,40}$/;
    if(!regexPW.exec(req.body['password']))
    {
        req.flash('error', '密码长度不小于6不大于40，请勿输入中文');
        return res.redirect('/reg');
    }

    var regexMail = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
    var result =  regexMail.exec(req.body.mail);
    if(!result)
    {
        req.flash('error', '请使用正确的邮箱[数字/字母或下划线]@[主机名].[密码] 如:uml@gmail.com');
        return res.redirect('/reg');
    }

    //生成md5的密码
    var md5 = crypto.createHash('md5');
    var password = md5.update(req.body.password).digest('base64');

    var sha1 = crypto.createHash('sha1');
    var time = new Date().toString();
    var link = sha1.update(time).digest('hex');

    var newUser = new User({
        //name: req.body.username,
        password: password,
        state: 0,
        mail : req.body.mail,
        link: link
    });

    //检查用户名是否已经存在
    User.get(newUser.mail, function(err, user) {
        if (user)
            err = '邮箱已存在';
        if (err) {
            req.flash('error', err);
            return res.redirect('/reg');
        }

        //如果不存在則新增用戶
        newUser.save(function(err) {
            if (err) {
                req.flash('error', err);
                return res.redirect('/reg');
            }
            //Smtp服务
            Smtp.send(newUser, 'checklink',function(err){
            });

            //返回数据
            req.session.user = {
                state:newUser.state,
                mail:newUser.mail
            };
            req.flash('success', '注册成功/reg');
            //res.redirect('/');
            res.redirect('/checkmail');
        });
    });
};

/*
 * login
 */
exports.login = function(req, res) {
    res.render('login', {
        title: '用户登录',
        user : req.session.user,
        success : req.flash('success').toString(),
        error : req.flash('error').toString()
    });
};

/*
 * doLogin
 */
exports.doLogin = function(req, res) {
    //生成口令的散列值
    var md5 = crypto.createHash('md5');
    var password = md5.update(req.body.password).digest('base64');

    User.get(req.body.mail, function(err, user) {
        if (!user) {
            req.flash('error', '用户不存在/login');
            return res.redirect('/login');
        }
        if (user.password != password) {
            req.flash('error', '密码错误/login');
            return res.redirect('/login');
        }
        req.session.user = {
            mail:   user.mail,
            state:  user.state,
            mail:   user.mail
        };
        if (user.state === 0)
        {
            req.flash('error', '用户未激活/login');
            return res.redirect('/checkmail');
        }
        req.flash('success', '登录成功/login');
        res.redirect('/u/'+req.body.mail);
    });
};

/*
 * doLogout
 */
exports.logout = function(req, res) {
    req.session.user = null;
    req.flash('success', '登出成功/logout');
    res.redirect('/');
};

/*
 * checkMail
 */
exports.checkMail = function(req,res){
    res.render('checkmail', {
        title: '用户激活',
        user : req.session.user,                    //aboute cookie
        success : req.flash('success').toString(),
        error : req.flash('error').toString()
    });
};

/*
 * doCheckMail
 */
exports.doCheckMail = function(req,res){
    var sha1 = crypto.createHash('sha1');
    var time = new Date().toString();
    var link = sha1.update(time).digest('hex');

    //检查用户名是否已经存在
    User.get(req.session.user.mail, function(err, user) {
        if (!user){
            err = '用户不存在.';
            req.flash('error', err);
            return res.redirect('/reg');
        };
        //数据更新与发送邮件
        user.link = link;
        user.updateLink(function(err) {
            if(err){
                req.flash('error', err);
                return res.redirect('/reg');
            }
        });
        //Smtp服务
        Smtp.send(user,'checklink', function(err){
        });

        //返回数据
        req.session.user = {
            state:user.state,
            mail:user.mail
        };
        req.flash('success', '链接已发送/doCheckMail');
        return res.redirect('/checkmail');
    });
};

/*
 * checkLink
 */
exports.checkLink = function(req,res){
    console.log("checkLink");
    User.get(req.params.user, function(err, user) {
        //console.log(user);
        console.log("checkInfo");
        if (!user) {
            console.log('用户不存在/checkLink');
            req.flash('error', '用户不存在/checkLink');
            return res.redirect('/');
        }
        if(req.params.link != user.link){
            console.log('无效链接/checkLink');
            req.flash('error', '无效链接/checkLink');
            return res.redirect('/');
        }
        if(user.state != 0){
            console.log('此用户已激活/checkLink');
            req.flash('error', '此用户已激活/checkLink');
            console.log(user);
            req.session.user = {
                mail:   user.mail,
                state:  user.state
            };

            return res.redirect('/');
        }
        //进行存储
        user.activate(function(err,user){
            console.log("activate");
            console.log(user);
            if(err){
                req.flash('error', err);
                return res.redirect('/');
            }

            req.session.user = {
                mail:   user.mail,
                state:  user.state
            };
            req.flash('success', '激活成功/checkLink');
            console.log(req.session.user);
            return res.redirect('/u/'+user.mail);
        });
    });
};

/*
 * forget
 */
exports.forget = function(req, res) {
    res.render('forget', {
        title: '重发密码',
        user : req.session.user,
        success : req.flash('success').toString(),
        error : req.flash('error').toString()
    });
};


/*
 * doforget
 */
exports.doForget = function(req, res) {
    var sha1 = crypto.createHash('sha1');
    var time = new Date().toString();
    var link = sha1.update(time).digest('hex');

    //检查用户名是否已经存在
    User.get(req.body.mail, function(err, user) {
        if (!user){
            err = '用户不存在.';
            req.flash('error', err);
            return res.redirect('/forget');
        };
        //数据更新与发送邮件
        user.link = link;
        user.updateLink(function(err) {
            if(err){
                req.flash('error', err);
                return res.redirect('/forget');
            }
        });
        //Smtp服务
        Smtp.send(user,'revisePW', function(err){
        });

        //返回数据
        req.session.user = {
            state:user.state,
            mail:user.mail
        };
        req.flash('success', '链接已发送/doforget');
        return res.redirect('/forget');
    });

};

/*
 * revisePW
 */
exports.revisePW = function(req,res){
    User.get(req.params.user, function(err, user) {
        if (!user) {
            req.flash('error', '用户不存在/checkLink');
            return res.redirect('/');
        }
        if(req.params.link != user.link){
            req.flash('error', '无效链接/checkLink');
            return res.redirect('/');
        }
        if(user.state != 1)
        {
            req.flash('error', '此用户未激活/checkLink');
            return res.redirect('/');
        }

        req.session.user = {
            mail:   user.mail,
            state:  user.state
        };
        res.render('revisePW', {
            title: "密码修改",
            user : req.session.user,      //用户仍然认为是登录用户，页面是选择的页面
            success : req.flash('success').toString(),
            error : req.flash('error').toString()
        });
    });
};

/*
 * dorevisePW
 */
exports.doRevisePW = function(req,res){
    var md5 = crypto.createHash('md5');
    var password = md5.update(req.body.password).digest('base64');

    User.get(req.session.mail, function(err, user) {
        if (!user) {
            req.flash('error', '用户不存在/login');
            return res.redirect('/login');
        }
        if (user.password != password) {
            req.flash('error', '密码错误/login');
            //return res.redirect('/login');
            return res.refresh();
        }

        if (user.state === 0)
        {
            req.flash('error', '用户未激活/login');
            return res.redirect('/checkmail');
        }
        //密码修改操作
        user.updatePW(function(err) {
            if(err){
                req.flash('error', err);
                return res.redirect('/reg');
            }
        });
        req.flash('success', '密码修改成功/login');
        res.redirect('/u/'+req.body.mail);
    });
};